package com.myfum.wingplayer.components.ui
{
	import com.myfum.wingplayer.components.containers.AbstractContainer;
	import com.myfum.wingplayer.components.decorators.AudioDecorator;
	import com.myfum.wingplayer.components.decorators.FlashDecorator;
	import com.myfum.wingplayer.components.decorators.PhotoDecorator;
	import com.myfum.wingplayer.components.decorators.TickerDecorator;
	import com.myfum.wingplayer.components.decorators.VideoDecorator;
	import com.myfum.wingplayer.entity.ContentType;
	import com.myfum.wingplayer.entity.RegionData;
	import com.myfum.wingplayer.entity.ScheduledContentData;
	import com.myfum.wingplayer.utils.ContentIterator;
	import com.myfum.wingplayer.utils.ReportManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	import gs.TweenLite;
	
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.ResizeEvent;
	
	import org.osmf.layout.AbsoluteLayoutFacet;
	
	public class Region extends UIComponent
	{
		private var _current:AbstractContainer;
		private var _prepare:AbstractContainer;
		private var _timer:Timer;
		private var _iterator:ContentIterator;
		private var _data:RegionData;
		private var _holder:UIComponent;
		private var _snapHolder:UIComponent;
		private var _masker:UIComponent;
		private var _snapShot:Bitmap;
		
		private var _lastTick:Number;
		private var _currentDuration:Number;
		private var _running:Boolean = false;
		private var _mute:Boolean = true;

		public function Region(mute:Boolean)
		{
			super();
			_mute = mute;
			init();
			initTracker();
		}
		
		private function init():void{
			_holder = new UIComponent();
			_masker = new UIComponent();
			_current = new AbstractContainer();
			_snapHolder = new UIComponent();
//			_prepare = new AbstractContainer();
			_holder.addChild(_current);
			addChild(_masker);
			_holder.mask = _masker;
			addChild(_holder);
			addChild(_snapHolder);
//			FlexGlobals.topLevelApplication.addEventListener(ResizeEvent.RESIZE, onResize);
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function onAdded(evt:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			fullScreen();
		}
		
		private function fullScreen():void{
			width = Math.floor(stage.fullScreenWidth*(data.width/100));
			height = Math.floor(stage.fullScreenHeight*(data.height/100));
			x = Math.floor(stage.fullScreenWidth*(data.left/100));
			y = Math.floor(stage.fullScreenHeight*(data.top/100));
			_current.width = width;
			_current.height = height;
			drawBg();
		}
		
		public function get data():RegionData
		{
			return _data;
		}
		
		public function set data(value:RegionData):void
		{
			_data = value;
			_iterator = new ContentIterator(data.arrayCollection.source);
			if(_iterator.length>0)startTicking();
		}
		
		public function set mute(value:Boolean):void{
			_mute = value;
		}
		
		public function get mute():Boolean{
			return _mute;
		}
		
		private function drawBg():void{
			with(graphics){
				beginFill(data.bgcolor,1);
				drawRect(0,0,width,height);
				endFill();
			}
			with(_masker.graphics){
				beginFill(0);
				drawRect(0,0,width,height);
				endFill();
			}
		}
		
		private function startTicking():void{
			_running = false;
			showSnapshot();
			var content:ScheduledContentData;
			if(_iterator.hasNext()){
				content = _iterator.next();
			}else{
				_iterator.reset();
				content = _iterator.next();
			}
			_currentDuration = content.duration*1000;
			/*_timer.delay = content.duration*1000;
			_timer.start();*/
			startPlay(content);
		}
		
		private function startPlay(content:ScheduledContentData):void{
			_running = true;
			_lastTick = getTimer();
			_current.dispose();
			_current.data = content;
			switch(content.category){
				case ContentType.TICKER:
					_current = new TickerDecorator(_current);
					break;
				case ContentType.VIDEO:
					_current = new VideoDecorator(_current);
					break;
				case ContentType.PHOTO:
					_current = new PhotoDecorator(_current);
					break;
				case ContentType.FLASH:
					_current = new FlashDecorator(_current);
					break;
				case ContentType.AUDIO:
					_current = new AudioDecorator(_current);
					break;
			}
			_current.mute = mute;
			_current.addEventListener(Event.COMPLETE, onComplete);
			if(width > 0 && height > 0){
				_current.width = width;
				_current.height = height;
			}
			_current.getReady();
			_current.play();
		}
		
		private function onComplete(evt:Event):void{
			_current.removeEventListener(Event.COMPLETE, onComplete);
			removeSnapshot();
			ReportManager.log("Show current content: " + _current.data.path);
//			TweenLite.delayedCall(.1, removeSnapshot);
		}
		
		private function initTracker():void{
			/*_timer = new Timer(1000);
			_timer.addEventListener(TimerEvent.TIMER, onTick);*/
			addEventListener(Event.ENTER_FRAME, onTick);
		}
		
		private function onTick(evt:Event):void{
			/*_timer.stop();*/
			if(_running){
				if(getTimer() - _lastTick >= _currentDuration){
					startTicking();
				}
			}
		}
		
		private function showSnapshot():void{
			while(_snapHolder.numChildren > 0){
				_snapHolder.removeChildAt(0);
			}
			if(_current.width > 0 && _current.height > 0){
				_snapShot = new Bitmap(new BitmapData(_current.width, _current.height, false));
				_snapShot.bitmapData.draw(this);
				_snapHolder.addChild(_snapShot);
				ReportManager.log("Show snapshot: " + _current.data.path);
			}
		}
		
		private function removeSnapshot():void{
			if(_snapShot && _snapHolder.contains(_snapShot)){
				_snapHolder.removeChild(_snapShot);
				ReportManager.log("remove snapshot");
				_snapShot.bitmapData.dispose();
				_snapShot = null;
			}
		}
		
		override public function set width(value:Number):void{
			super.width = value;
			_current.width = value;
		}
		
		override public function set height(value:Number):void{
			super.height = value;
			_current.height = value;
		}
		
		public function dispose():void{
			if(_timer){
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, onTick);
			}
			while(_holder.numChildren > 0){
				var c:AbstractContainer = _holder.removeChildAt(0) as AbstractContainer;
				c.dispose();
				c = null;
			}
		}
	}
}









